package com.chenjinyan.user.controller;

import com.alibaba.fastjson.JSON;
import com.chenjinyan.user.entity.UserEntity;
import com.chenjinyan.user.service.UserService;
import com.chenjinyan.user.utils.ExceptionEnum;
import com.chenjinyan.user.utils.ExceptionHandle;
import com.chenjinyan.user.utils.ResultUtil;
import com.chenjinyan.user.vo.Result;
import com.chenjinyan.user.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author chenjinyan
 * @version 1.0
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    private ExceptionHandle exceptionHandle;

    @PostMapping("/signup")
    public Result insertUser(@Valid @RequestBody User user, BindingResult bindingResult) {
        Result result = ResultUtil.success();
        try {
            //参数校验
            if (bindingResult.hasErrors()) {
                List<ObjectError> errorList = bindingResult.getAllErrors();
                for (ObjectError error : errorList) {
                    ExceptionEnum.PARAMETER_WRONG.setMsg(error.getDefaultMessage());
                    return ResultUtil.error(ExceptionEnum.PARAMETER_WRONG);
                }
            }
            //保存用户
            int i = userService.insertUser(user);
            if (i < 0) {
                result = ResultUtil.error(ExceptionEnum.USER_IS_FIND);
            }
        } catch (Exception e) {
            result = exceptionHandle.exceptionGet(e);
        }

        return result;
    }


    @PostMapping("/update")
    public Result updateUser(@RequestBody User user) {
        Result result = ResultUtil.success();
        if (null == user.getId()) {
            result = ResultUtil.error(ExceptionEnum.USER_ID_IS_NULL);
            return result;
        }
        try {
            //查找用户
            UserEntity userEntity = userService.findById(user.getId());
            if (null == userEntity) {
                result = ResultUtil.error(ExceptionEnum.USER_IS_NULL);
                return result;
            }
            int i = userService.updateUser(user);
            return result;
        } catch (Exception e) {
            result = exceptionHandle.exceptionGet(e);
            return result;
        }
    }

    @GetMapping("/signloginacc/{loginAccount}")
    public Result checkByLoginAcc(@PathVariable("loginAccount") String loginAccount) {
        Result result = ResultUtil.success();
        try {
            int i = userService.checkByLoginAcc(loginAccount);
            if (i < 0) {
                result = ResultUtil.error(ExceptionEnum.LOGIN_ACCOUNT_FOUND);
            }
        } catch (Exception e) {
            result = exceptionHandle.exceptionGet(e);
        }
        return result;
    }

    @GetMapping("/signphone/{phone}")
    public Result checkByPhone(@PathVariable("phone") String phone) {
        Result result = ResultUtil.success();
        try {
            int i = userService.checkByPhone(phone);
            if (i < 0) {
                result = ResultUtil.error(ExceptionEnum.PHONE_IS_FUND);
            }
        } catch (Exception e) {
            result = exceptionHandle.exceptionGet(e);
        }
        return result;
    }

    @GetMapping("/signemail/{email:.+}")
    public Result checkByEmail(@PathVariable("email") String email) {
        Result result = ResultUtil.success();
        try {
            int i = userService.checkByEmail(email);
            if (i < 0) {
                result = ResultUtil.error(ExceptionEnum.EMAIL_IS_FUND);
            }
        } catch (Exception e) {
            result = exceptionHandle.exceptionGet(e);
        }
        return result;
    }


    @GetMapping("/getUserInfo/{id}")
    public Result getUserInfo(@PathVariable("id") Long id) {
        Result result = ResultUtil.success();
        try {
            //查找用户
            List<UserEntity> list = userService.getUserInfo(id);
            if (null == list || list.size() == 0) {
                result = ResultUtil.error(ExceptionEnum.USER_IS_NULL);
                return result;
            }
            for (UserEntity userEntity : list) {
                userEntity.setPassword("");
            }
            String data = JSON.toJSONString(list);
           /* Map<String, String> map = new HashMap<>();
            map.put("data", data);*/
            result.setData(JSON.toJSONString(list));
            return result;
        } catch (Exception e) {
            result = exceptionHandle.exceptionGet(e);
        }
        return result;
    }

}
